package com.hs.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.hs.room.dao.AlarmLogDao
import com.hs.room.dao.ArchiveApprovalDao
import com.hs.room.dao.ArchiveChangeDao
import com.hs.room.dao.ArchiveDao
import com.hs.room.dao.ArchiveLevelDao
import com.hs.room.dao.ArchiveLogDao
import com.hs.room.dao.ArchiveStatusDao
import com.hs.room.dao.ArchiveTypeDao
import com.hs.room.dao.CaseDao
import com.hs.room.dao.DevCabinetDao
import com.hs.room.dao.DevReaderDao
import com.hs.room.dao.MaterialsDao
import com.hs.room.dao.MaterialsLevelDao
import com.hs.room.dao.MaterialsTypeDao
import com.hs.room.dao.OptLogDao
import com.hs.room.dao.SysAppointmentDao
import com.hs.room.dao.SysDeptDao
import com.hs.room.dao.SysLogDao
import com.hs.room.dao.SysRoleDao
import com.hs.room.dao.UserDao
import com.hs.room.model.archive.AlarmLog
import com.hs.room.model.archive.Archive
import com.hs.room.model.archive.ArchiveApproval
import com.hs.room.model.archive.ArchiveChange
import com.hs.room.model.archive.ArchiveLevel
import com.hs.room.model.archive.ArchiveLog
import com.hs.room.model.archive.ArchiveStatus
import com.hs.room.model.archive.ArchiveType
import com.hs.room.model.archive.Case
import com.hs.room.model.archive.DevCabinet
import com.hs.room.model.archive.DevReader
import com.hs.room.model.archive.OptLog
import com.hs.room.model.material.Materials
import com.hs.room.model.material.MaterialsLevel
import com.hs.room.model.material.MaterialsType
import com.hs.room.model.sys.SysAppointment
import com.hs.room.model.sys.SysDept
import com.hs.room.model.sys.SysLog
import com.hs.room.model.sys.SysRole
import com.hs.room.model.sys.SysUser

@Database(
    entities = [
        SysUser::class,
        SysLog::class,
        SysDept::class,
        SysRole::class,
        SysAppointment::class,
        Materials::class,
        MaterialsLevel::class,
        MaterialsType::class,
        ArchiveType::class,
        ArchiveLevel::class,
        ArchiveStatus::class,
        Archive::class,
        ArchiveApproval::class,
        ArchiveChange::class,
        ArchiveLog::class,
        Case::class,
        DevCabinet::class,
        DevReader::class,
        OptLog::class,
        AlarmLog::class,
    ], version = BuildConfig.DBVersion
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
    abstract fun sysRoleDao(): SysRoleDao
    abstract fun sysLogDao(): SysLogDao
    abstract fun sysDeptDao(): SysDeptDao
    abstract fun sysAppointmentDao(): SysAppointmentDao
    abstract fun MaterialsDao(): MaterialsDao
    abstract fun materialsTypeDao(): MaterialsTypeDao
    abstract fun materialsLevelDao(): MaterialsLevelDao
    abstract fun archiveTypeDao(): ArchiveTypeDao
    abstract fun archiveLevelDao(): ArchiveLevelDao
    abstract fun archiveStatusDao(): ArchiveStatusDao
    abstract fun alarmLogDao(): AlarmLogDao
    abstract fun archiveApprovalDao(): ArchiveApprovalDao
    abstract fun archiveChangeDao(): ArchiveChangeDao
    abstract fun archiveDao(): ArchiveDao
    abstract fun archiveLogDao(): ArchiveLogDao
    abstract fun caseDao(): CaseDao
    abstract fun devCabinetDao(): DevCabinetDao
    abstract fun devReaderDao(): DevReaderDao
    abstract fun optLogDao(): OptLogDao


    companion object {

        // TODO: 升级数据库需要添加migration
//        val MIGRATION_3_4 = Migration(3, 4) { database ->
//            database.execSQL("CREATE TABLE archive_type (localId INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, atName TEXT, atRemarks TEXT, atSort INTEGER, uuid TEXT, enable INTEGER)")
//            database.execSQL("CREATE TABLE archive_level (localId INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, alName TEXT, alRemarks TEXT, alSort INTEGER, uuid TEXT, enable INTEGER)")
//            database.execSQL("CREATE TABLE archive_status (localId INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER, asName TEXT, asRemarks TEXT, asSort INTEGER, uuid TEXT, enable INTEGER)")
//        }

        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(
                context.applicationContext,
                AppDatabase::class.java,
                BuildConfig.DBName
            ).allowMainThreadQueries()
//                .addMigrations(MIGRATION_3_4) // 添加迁移类
                .build()
        }
    }
}